/*
题目链接：https://leetcode.cn/problems/diameter-of-binary-tree/description/
	刘沛民	2024-12-3
*/

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int Max;
    int undertracking(TreeNode* node){ 
        if (node == nullptr) return 0;
        int leftLength = undertracking(node->left);
        int rightLength = undertracking(node->right);

        Max = abs(leftLength+rightLength)>Max? abs(leftLength+rightLength) : Max; 
        return max(leftLength, rightLength)+1; 
    }
    int diameterOfBinaryTree(TreeNode* root) {
        if (root == nullptr) return 0;

        Max = 0;
        undertracking(root);

        return Max;
    }
};
